import React from "react";
import {Tooltip} from 'antd'
/**
 * Created by coder on 2018/3/29.
 */
export function WtStringFormat(str, args){
	let result = str;
	if(arguments.length > 1){
		if(arguments.length == 2 && typeof(args) == 'object'){
			for(let key in args){
				if(args[key] != undefined){
					let reg = new RegExp("({" + key + "})", "g");
					result = result.replace(reg,args[key]);
				}
			}
		} else {
			for(let i=1;i<arguments.length;i++){
				if(arguments[i] != undefined){
					var reg = new RegExp("({)" + (i-1) + "(})", "g");
					result = result.replace(reg, arguments[i]);
				}
			}
		}
	}
	return result;
}


export function fileSizeFormat(size){
	if(size > 1024*1024*1024){
		return (size/(1024*1024*1024)).toFixed(2) + 'G';
	} else if(size > 1024 *1024){
		return (size/(1024*1024)).toFixed(2) + 'M';
	} else if(size > 1024){
		return (size/1024).toFixed(2) + 'K';
	} else {
		return size + 'B';
	}
}

export function lengthControlForTable(content){
        return (
        	<Tooltip placement="topLeft" title={content}>
				<span>{content}</span>
			</Tooltip>
        );
}

export function renderLongString(content){
    if(!content){
        return null;
    }
    const count = content.length / 50;
    let newContent = [];
    for(let i=0;i<count;i++){
        let subContent = content.slice(i*50,(i+1)*50);
        newContent.push(subContent);
        newContent.push(<br/>);
    }
    return (
        <code style={{display:'block',marginLeft:120,marginTop:-24}}>
            {newContent}
        </code>
    );
}
